import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpClient3 {
    private DatagramSocket socket=null;
    String serveip=null;
    int port=0;
    public UdpClient3(String serveip,int port) throws SocketException {
        socket=new DatagramSocket();
        this.serveip=serveip;
        this.port=port;

    }
    public void start() throws IOException {
        System.out.println("客户端启动");
        Scanner scanner=new Scanner(System.in);
        while(true)
        {
            String request=scanner.next();
            if(request.equals("goodbye"))
            {
                System.out.println("客户端下线");
                break;
            }
            DatagramPacket datagramPacket=new DatagramPacket(request.getBytes(),request.getBytes().length,
                    InetAddress.getByName(serveip),port);
            socket.send(datagramPacket);
            DatagramPacket datagramPacket1=new DatagramPacket(new byte[5000],5000);
            socket.receive(datagramPacket1);
            String resonse=new String(datagramPacket1.getData(),0, datagramPacket.getLength());
            System.out.println(resonse);


        }
    }
    public static void main(String[] args) throws IOException {
        UdpClient3 udpClient3=new UdpClient3("192.168.1.101",10002);
        udpClient3.start();


    }
}
